<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MY_Controller {
	public function __construct(){
		parent::__construct();
		//$this->config->set_item('enable_query_strings',TRUE);
		$this->load->model('User_model','Muser');
	}
	
	/**
	 * user list
	 * 
	 * @param integer $pindex current page
	 */
	public function index($pindex=1){
		$this->load->library('pagination');
		$this->load->config('pagination');
		$config['base_url'] = site_url('user/index/');
		$config['total_rows'] = $this->Muser->count_all();
		$config['per_page'] = $per_page = $this->config->item('per_page');
		$this->pagination->initialize($config);
		$per_page = $this->config->item('per_page');
		
		$data['pagination'] = $this->pagination->create_links();
		$offset = $per_page * ($pindex-1);
		$this->Muser->db->from($this->Muser->tablename);
		$this->Muser->db->order_by('uid');
		$this->Muser->db->limit($per_page,$offset);
		$users = $this->Muser->db->get()->result_object();
		//echo $this->db->last_query();
		 		
 		$data['users'] = $users;
		$this->display('user_index',$data);
	}
	
	public function image(){
		header('Content-Type:image/jpeg');
		$filename = "myemm/theme/images/avatar2.jpg";
		$handle = fopen($filename, "rb");
		$filesize = filesize($filename);
		$contents = fread($handle, $filesize);
		
		fclose($handle);
	}
	
	private function get_per_page(){
		$page_sizes = array(30,50,100); 
		$custom_page = (int)$this->input->get('per_page');
		if (in_array($custom_page, $page_sizes)){
			$per_page = $custom_page;
		}
		else{
			$this->config->load('pagination');
			$per_page = $this->config->item('per_page');
		}
		return $per_page;
	}
}